package com.youpai.platform.user.service.converter;

import com.youpai.platform.common.util.StringUtils;
import com.youpai.platform.user.web.entity.dto.UserDTO;
import com.youpai.platform.user.service.entity.po.UserPO;
import com.youpai.platform.user.web.entity.req.UpdateUserReq;
import org.springframework.util.CollectionUtils;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author welles
 * @version 1.0
 * @description 用户service转换类
 * @date 2021/10/7
 */
public class UserConverter {

    /**
     * 将UserPO转换成UserDTO
     *
     * @param userPO UserPO
     * @return UserDTO
     */
    public static UserDTO convertUserPO2UserDTO(final UserPO userPO) {
        UserDTO userDTO = new UserDTO();
        if (null == userPO) {
            return userDTO;
        }
        userDTO.setAvatar(userPO.getAvatar())
                .setBirthday(userPO.getBirthday())
                .setEmail(userPO.getEmail())
                .setMobile(userPO.getMobile())
                .setNickName(userPO.getNickName())
                .setAvatar(userPO.getAvatar())
                .setUid(userPO.getUid().toString())
                .setSummary(userPO.getSummary())
                .setGender(userPO.getGender())
                .setUserName(userPO.getUserName())
                .setUserType(userPO.getUserType());
        return userDTO;
    }

    /**
     * 将UpdateUserReq转换成userPO
     *
     * @param req req
     * @return userPO
     */
    public static UserPO convertUpdateUserReq2UserPO(final UpdateUserReq req) {
        UserPO userPO = new UserPO();
        if (null == req) {
            return userPO;
        }
        Date birthDay = StringUtils.parseDate(req.getBirthday());
        userPO.setAvatar(req.getAvatar())
                .setBirthday(birthDay)
                .setEmail(req.getEmail())
                .setNickName(req.getNickName())
                .setSummary(req.getSummary())
                .setGender(req.getGender());
        return userPO;
    }

    public static List<UserDTO> convertUserPOList2UserDTOList(final List<UserPO> userPOList) {
        if (CollectionUtils.isEmpty(userPOList)) {
            return Collections.emptyList();
        }
        List<UserDTO> userDTOList = new ArrayList<>(userPOList.size());
        for (UserPO userPO : userPOList) {
            userDTOList.add(convertUserPO2UserDTO(userPO));
        }
        return userDTOList;
    }
}
